import sys
from PyQt5.QtWidgets import QSlider, QApplication, QVBoxLayout, QHBoxLayout, QWidget, QLabel
from PyQt5.QtCore import Qt

class MediaPlayerControls(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()
        self.setup_media_controls()
    
    def setup_ui(self):
        layout = QVBoxLayout()
        
        # 进度条滑块
        self.progress_slider = QSlider(Qt.Horizontal)
        self.progress_slider.setRange(0, 1000)
        self.progress_slider.setValue(0)
        self.progress_slider.setTickPosition(QSlider.NoTicks)
        
        # 时间显示
        self.time_layout = QHBoxLayout()
        self.current_time = QLabel("00:00")
        self.total_time = QLabel("00:00")
        self.time_layout.addWidget(self.current_time)
        self.time_layout.addStretch()
        self.time_layout.addWidget(self.total_time)
        
        # 音量控制
        volume_layout = QHBoxLayout()
        volume_layout.addWidget(QLabel("音量:"))
        self.volume_slider = QSlider(Qt.Horizontal)
        self.volume_slider.setRange(0, 100)
        self.volume_slider.setValue(80)
        self.volume_slider.setFixedWidth(100)
        volume_layout.addWidget(self.volume_slider)
        
        layout.addWidget(self.progress_slider)
        layout.addLayout(self.time_layout)
        layout.addLayout(volume_layout)
        self.setLayout(layout)
    
    def setup_media_controls(self):
        # 进度条信号
        self.progress_slider.sliderPressed.connect(self.on_progress_pressed)
        self.progress_slider.sliderMoved.connect(self.on_progress_moved)
        self.progress_slider.sliderReleased.connect(self.on_progress_released)
        
        # 音量信号
        self.volume_slider.valueChanged.connect(self.on_volume_changed)
    
    def on_progress_pressed(self):
        """进度条按下 - 暂停播放"""
        print("暂停播放，准备跳转")
    
    def on_progress_moved(self, value):
        """进度条移动 - 预览位置"""
        time_str = self.format_time(value)
        self.current_time.setText(time_str)
    
    def on_progress_released(self):
        """进度条释放 - 跳转到指定位置"""
        target_position = self.progress_slider.value()
        print(f"跳转到位置: {target_position}")
        # 实际媒体播放器的跳转逻辑
    
    def on_volume_changed(self, value):
        """音量改变"""
        print(f"音量设置为: {value}%")
        # 实际音量控制逻辑
    
    def format_time(self, seconds):
        """格式化时间显示"""
        mins = seconds // 60
        secs = seconds % 60
        return f"{mins:02d}:{secs:02d}"
    
    def update_progress(self, current, total):
        """更新进度（从播放器调用）"""
        self.progress_slider.setRange(0, total)
        self.progress_slider.setValue(current)
        self.current_time.setText(self.format_time(current))
        self.total_time.setText(self.format_time(total))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MediaPlayerControls()
    window.show()
    sys.exit(app.exec_())